package com.bzsg.rightsManagementSystem.dao

import com.bzsg.rightsManagementSystem.pojo.RequestPath
import org.apache.ibatis.annotations.Delete
import org.apache.ibatis.annotations.Insert
import org.apache.ibatis.annotations.Mapper
import org.apache.ibatis.annotations.Param
import org.apache.ibatis.annotations.Select
import org.apache.ibatis.annotations.Update

@Mapper
interface RequestPathDao {
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Select("select * from request_path where id = #{id}")
    fun queryById(id: Int): RequestPath?

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Select("select * from request_path limit #{offset}, #{limit}")
    fun queryAllByLimit(@Param("offset") offset: Int, @Param("limit") limit: Int): List<RequestPath>?

    /**
     * 通过实体作为筛选条件查询
     *
     * @param requestPath 实例对象
     * @return 对象列表
     */
    @Select("select * from request_path")
    fun queryAll(): List<RequestPath>?

    /**
     * 新增数据
     *
     * @param requestPath 实例对象
     */
    @Insert(
        "   insert into request_path(url, description)\n" +
                "        values (#{url}, #{description})"
    )
    fun insert(requestPath: RequestPath)

    /**
     * 修改数据
     *
     * @param requestPath 实例对象
     */
    @Update(
        "     <script>\n" +
                "        update request_path\n" +
                "        <set>\n" +
                "            <if test=\"url != null and url != ''\">\n" +
                "                url = #{url},\n" +
                "            </if>\n" +
                "            <if test=\"description != null and description != ''\">\n" +
                "                description = #{description},\n" +
                "            </if>\n" +
                "        </set>\n" +
                "        where id = #{id}\n" +
                "      </script>"
    )
    fun update(requestPath: RequestPath)

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @Delete("delete from request_path where id = #{id}")
    fun deleteById(id: Int)

    /**
     * 选择所有请求路径数目
     *
     * @return 数目
     */
    @Select("SELECT COUNT(*) FROM request_path")
    fun selectTotalRequestPath(): Int

    /**
     * 查询权限下具有的所有请求路径
     *
     * @param permissionId
     * @return 请求路径列表
     */
    @Select(
        " select r.* from request_path as r ,permission_path_relation AS rp\n" +
                " where rp.permission_id=#{permissionId} and r.id=rp.path_id"
    )
    fun selectPathByPermissionId(permissionId: Int): List<RequestPath>?
}